/*
 *                    COPYRIGHT NOTICE
 *
 *           This code is copyright protected by Freedom Inc.
 *
 *              2001-2012 All Rights Are Reserved
 */
package com.freedom.oldsong.spring;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.ClassUtils;
import org.w3c.dom.Element;

import com.freedom.oldsong.system.dao.impl.BaseDaoImpl;

/**
 * @author steven.wang
 *
 */
public class DaoBeanDefinitionParser implements BeanDefinitionParser {
	
	protected final Log log = LogFactory.getLog(DaoBeanDefinitionParser.class);
	public BeanDefinition parse(Element element, ParserContext parserContext) {
		// create a RootBeanDefinition that will serve as configuration
		// holder for the 'pattern' attribute and the 'lenient' attribute
		RootBeanDefinition targetBeanDef = new RootBeanDefinition();

		AbstractBeanDefinition beanDef = new RootBeanDefinition(
				ProxyFactoryBean.class);
		try {
			String target = element.getAttribute("target");
			String interfaceName = element.getAttribute("interfaces");
			if(target==null || "".equals(target.trim())){
				targetBeanDef.setBeanClass(BaseDaoImpl.class);
			}else{
				targetBeanDef.setBeanClass(Class.forName(target));
				if(interfaceName==null){
					interfaceName=(String)targetBeanDef.getPropertyValues().getPropertyValue("interfaces").getValue();
				}
			}
			targetBeanDef.getPropertyValues().addPropertyValue("dataSource", parserContext.getRegistry().getBeanDefinition("dataSource"));
			targetBeanDef.getPropertyValues().addPropertyValue("sqlMapClient",parserContext.getRegistry().getBeanDefinition("sqlMapClient"));
			
			// beanDef.setBeanClass(LogBean.class);
			// never null since the schema requires it
		
			beanDef.getPropertyValues().addPropertyValue("interfaces",interfaceName);
			beanDef.getPropertyValues().addPropertyValue("target",targetBeanDef);
			targetBeanDef.getPropertyValues().addPropertyValue("pojoClass", GenericsUtils.getGenericClass(ClassUtils.forName(interfaceName)));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
		} catch (LinkageError e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
		}
		// retrieve the ID attribute that will serve as the bean identifier in
		// the context
		String id = element.getAttribute("id");
		// create a bean definition holder to be able to register the
		// bean definition with the bean definition registry
		// (obtained through the ParserContext)
		BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDef, id);
		// register the BeanDefinitionHolder (which contains the bean
		// definition)
		// with the BeanDefinitionRegistry
		BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext
				.getRegistry());
		return beanDef;
	}
}